
package com.vanda.util;
/**
 * @author ls
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class CheckUtil {
	/**
	 * 编码检查，注意不是标志符检查
	 * @param test
	 * @return
	 */
	public static void checkNotNull(String value, String caption)
		throws Exception {
		if (value == null)
			throw new Exception(caption + "不能为null");
		value = value.trim();
		if (value.length() == 0)
			throw new Exception(caption + "不能为空");
	}
	public static void checkStrCode(String value, String caption)
		throws Exception {
		value = value.trim();
		for (int i = 0; i < value.length(); i++) {
			char ch = value.charAt(i);
			if ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"
				.indexOf(ch)
				< 0)
				throw new Exception(caption + "含有非法字符");
		}
	}
	public static void checkNumCode(String value, String caption)
		throws Exception {
		value = value.trim();
		for (int i = 0; i < value.length(); i++) {
			char ch = value.charAt(i);
			if ("0123456789.".indexOf(ch) < 0)
				throw new Exception(caption + "含有非法字符");
		}
	}
	public static void checkLength(
		String value,
		String caption,
		int min,
		int max)
		throws Exception {
		value = value.trim();
		if (min == max) {
			if (value.length() != min)
				throw new Exception(caption + "长度必须是" + min);
		} else if (value.length() > max || value.length() < min) {
			throw new Exception(caption + "长度必须大于" + min + ",小于" + max);
		}
	}
	public static void checkScope(
		String value,
		String caption,
		double min,
		double max)
		throws Exception {
		value = value.trim();
		double dv = new Double(value).doubleValue();
		if (dv > max || dv < min)
			throw new Exception(caption + "必须大于" + min + ",然后必须小于" + max);
	}
}
